<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .boss {
      width: 400px;
      height: 600px;
      background-color: black;
      border-radius: 5%;
      position: absolute;
      left: 50px;
      top: 50px;
      /* display: none; */
    }



    .box {
      width: 400px;
      height: 50px;
      background-color: aqua;
      position: absolute;
      left: 50px;
      top: 500px
    }

    .round {
      width: 50px;
      height: 50px;
      background-color: #fff;
      border-radius: 50%;
      position: absolute;
      /* left: 0; */
      /* top: 500px */
    }
  </style>
</head>

<body>
  <div class="boss"> </div>
  <div class="box">
    <div class="round"></div>
  </div>
</body>
<script>
  var boss = document.getElementsByClassName('boss')[0];
  var box = document.getElementsByClassName('box')[0];
  var round = document.getElementsByClassName('round')[0];

  // onmousedown  先按下,记录按下的位置(移动需要)
  // onmousemove  再移动, 元素跟随鼠标移动 
  // onmouseup    再松开, 元素停留在当前位置


  //  onmousedown  先按下,记录按下的位置(移动需要)
  var maxWidth = box.clientWidth - round.offsetWidth
  box.onmousedown = function (e) {
    // console.log(1111);
    var e = e || window.event;
    console.log(e);

    var posX = e.offsetX;
    var posY = e.offsetY;
    console.log(posX, posY);


    document.onmousemove = function () {
      var e = e || window.event;
      var x = e.pageX - posX - boss.offsetLeft;
      // var y = e.pageY - posY;
      if (x < 0) {
        x = 0;
      }
      if (x >= maxWidth) {
        x = maxWidth;
      }
      round.style.left = x + "px";
    }

    document.onmouseup = function () {
      document.onmousemove = null;
      if (round.offsetLeft == maxWidth) {
        // boss.style.display = 'block';
        window.open("http://www.baidu.com");
      } else {
        round.style.left = 0 + 'px';
      }
    }

  }


</script>

</html>